import addSelButton from "@/components/diy-form/add-sel-button.vue";
import addSelBg from "@/components/diy-form/add-sel-bg.vue";
import selForm from "@/components/diy-form/sel-form.vue";
import showForm from "@/components/diy-form/show-form.vue";
import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue";
import shareWechat from '@/components/share/share-wechat.vue';
export default {
	components:{
		addSelBg,
		addSelButton,
		selForm,
		showForm,
		MxDatePicker,
		shareWechat
	},
	data() {
		return {
			temp_id:0,
			loginStatus:false,
			userInfo:{},
			mobile:'',
			title:'',
			formData:[],
			//日期时间选择数据
			mxDatePicker:{
				showPicker: false,
				type: '',
				start_time: '',
				end_time:'',
				index:-1,
			},
			againMore:-1,
			showPushCircletip:0,
			pushCircleStatus:-1,
			pushCircleList:[],
			push_circle_index:0,
			joinRuleList:[],
			joinRuleListIndex:0,
			circle_id:0,
			s_config:{},
			loginCallBackType:'', 
		}
	},
	onLoad(e){
		this.joinRuleList = this.$api.joinRuleList();
		//初始化时间
		var initTime = this.$api.getDateRange('',7,5);
		this.mxDatePicker.start_time = initTime[0]?initTime[0]:'';
		this.mxDatePicker.end_time = initTime[1]?initTime[1]:'';
		if(e.temp_id)
		{
			this.temp_id = e.temp_id;
			this.getTempInfo();
		}
		if(e.circle_id)
		{
			this.circle_id = e.circle_id;
			this.pushCircleStatus = 1;
			this.getCircleList();
		}
		
	},
	onShow(){
		this.onShowInit();
		//检测是不是登录了
		if(this.$refs.show_form)
		{
			this.$refs.show_form.parentSetUsername();
		}
	},
	onShareAppMessage(){
		var userInfo = this.$api.checkUserInfo();
		var nickname = userInfo.nickname?userInfo.nickname:'';
		var title = nickname+(this.temp_id > 0?'邀请您使用模板发布统计':'邀请您发布统计');
		var path = '/pages/form/statistics/add?temp_id='+this.temp_id;
		var shareObj = {
			title:title,
			path:path
		}
		var imageUrl = '';
		try{
			imageUrl = this.$refs.add_sel_bg.getShowPath();
		}catch(e){
			
		}
		shareObj.imageUrl = imageUrl?imageUrl:this.$api.getSystemConfig('group_share_bg');
		return shareObj;
	},
	methods: {
		wechatRegister(e){
			if(e.code == 1)
			{
				var that = this;
				that.onShowInit();
				switch(that.loginCallBackType)
				{
					case 'pushCircleChange':
						that.pushCircleChange();
						break;
					case 'submit':
						that.submit();
						break;
				}
				that.loginCallBackType = '';
			}
		},
		onShowInit(){
			this.loginStatus = this.$api.checkLogin(false);
			this.userInfo = this.$api.checkUserInfo();
			if(this.temp_id > 0)
			{
				this.getStatisticsConfig();
			}
		},
		getStatisticsConfig()
		{ 
			var that = this;
			this.$http.post('config/getStatisticsConfig',{
				type:1,
				temp_id:that.temp_id
			}).then(res=>{
				that.s_config = res.data;
			})
		},
		shareFunc(){
			var userInfo = this.$api.checkUserInfo();
			var title = (this.temp_id > 0?'邀请您使用模板发布统计':'邀请您发布统计');
			var posterData = {
				title:title,
				nickname:userInfo.nickname?userInfo.nickname:'',
				qrcodeUrl:this.s_config.uni_qrcode,
				path:'/pages/form/statistics/add?temp_id='+this.temp_id
			};
			this.$refs.share_wechat.setShowStatus(true,posterData);
		},
		getTempInfo()
		{
			var that = this;
			that.$api.loadingMsg('正在加载模板数据');
			that.$http.post('Template/getTempInfo',{
				temp_id:that.temp_id
			}).then(res=>{
				if(res.code == 1)
				{
					var info = res.data.info;
					that.title = info.title;
					if(info.bgPath)
					{
						that.$refs.add_sel_bg.initBgFunc(info.bgPath);
					}
					if(info.join_datalist)
					{
						that.formData = JSON.parse(JSON.stringify(info.join_datalist))
					}
					if(info.content_list)
					{
						let join_contentlist = JSON.parse(JSON.stringify(info.content_list));
						that.$refs.add_sel_button.initDataList(join_contentlist)
					} 
				}
			})
		},
		againMoreChange(e){
			var status = e.detail.value;
			this.againMore = (status == true)?1:-1;
		},
		pushCircleChange(){
			var that = this;
			var checkLogin = that.$api.checkLogin(false);
			if(checkLogin == false)
			{
				that.pushCircleStatus = -1;
				that.loginCallBackType = 'pushCircleChange';
				that.$refs.wechat_login.saveShowStatus(true);
				return ;
			}
			var status = (this.pushCircleStatus == -1)?1:-1;
			if(status == 1 && that.showPushCircletip < 1){
				uni.showModal({
					title:'友情提示',
					content:'只能选择一个圈子，选择圈子发布后不能再次修改!',
					showCancel:false
				})
				that.showPushCircletip++;
			}
			if(status == 1 && this.pushCircleList.length <1)
			{
				this.getCircleList();
			}
			this.pushCircleStatus =status;
		},
		getCircleList(){
			var that = this;
			that.$http.post('config/getCreateCircleList').then(res=>{
				that.push_circle_index = 0;
				if(res.code == 1)
				{
					var resDataList = res.data.list;
					that.pushCircleList = resDataList;
					if(that.circle_id > 0)
					{
						for(var i=0;i<resDataList.length;i++)
						{
							if(resDataList[i].circle_id == that.circle_id)
							{
								that.push_circle_index = i;
								break;
							}
						}
					}
				}else{
					that.pushCircleList = [];
					that.$api.msg(res.msg);
				}
			});
		},
		bindPickerChange(e){
			this.push_circle_index = e.detail.value;
		},
		joinRuleChange(e){
			this.joinRuleListIndex = e.detail.value;
			console.log(this.joinRuleListIndex)
		},
		DatePicker(type,index){//显示
			this.mxDatePicker.index = index;
			this.mxDatePicker.type = type; 
			this.mxDatePicker.showPicker = true;
			if(index == 1)
			{
				var time = this.mxDatePicker.start_time;
			}else{
				var time = this.mxDatePicker.end_time;
			}
			this.mxDatePicker.value = time;
			//this.mxDatePicker.value  = '';
			//this.$forceUpdate();
		},
		DatePickerEnd(e) {//选择
			this.mxDatePicker.showPicker = false;
			if(e) {
				if(this.mxDatePicker.index == 1)
				{
					this.mxDatePicker.start_time = e.value;
				}else{
					this.mxDatePicker.end_time = e.value;
				}
			}
		},
		previewForm(){
			var bgPath = this.$refs.add_sel_bg.getShowPath();
			var infoParams = {
				bgPath:{
					path:bgPath,
					qnPath:bgPath
				},
				avatar:this.userInfo.avatar?this.userInfo.avatar:'',
				nickname:this.userInfo.nickname?this.userInfo.nickname:'',
				mobile:this.mobile,
				title:this.title,
				start_time:this.mxDatePicker.start_time,
				end_time:this.mxDatePicker.end_time,
				contentList:this.$refs.add_sel_button.getPreviewContent(),
			};
			if(infoParams.title == '')
			{
				this.$api.msg('名称标题不能为空');return ;
			}
			var dataList = this.$refs.show_form.getItemList();
			if(dataList.length > 0)
			{
				var opeList = this.$api.opeDiyFormData(dataList);
				if(opeList.length < 1)
				{
					return;
				}
			}else{
				var opeList = [];
			}
			infoParams.opeList = opeList;
			//console.log(infoParams)
			uni.setStorageSync('form-statistics-detail-preview-data',infoParams);
			this.$api.navigateTo('/pages/form/statistics/detail-preview'); 
			/* uni.setStorageSync('common-preview-cache-data',opeList);
			this.$api.navigateTo('/pages/common/preview-form'); */
		},
		showSelForm(){
			this.$refs.sel_form.tggleStatus(true);
		},
		selFormData:function(info){
			if(info)
			{
				this.$refs.show_form.changeItemlist();
				this.formData.push(info);
			}
		},
		selFormDataChange(data){
			this.formData = data;
		},
		inputMobile(){
			var that = this;
			uni.showModal({
				title:'输入手机号',
				content:that.mobile,
				editable:true,
				placeholderText:'',
				success(res){
					if(res.confirm)
					{
						var checkMobile = that.$api.isPhoneNumber(res.content);
						if(checkMobile == false)
						{
							that.mobile = '';
							that.$api.msg('请输入正确的手机号');
						}else{
							that.mobile = res.content;
						}
					}
				}
			})
		},
		
		async submit(){
			var that = this;
			let postParams = {};
			//判断是否登录
			var checkLogin = that.$api.checkLogin(false);
			if(checkLogin == false)
			{
				that.loginCallBackType = 'submit';
				that.$refs.wechat_login.saveShowStatus(true);
				return ;
			}
			//判断标题
			postParams.title = that.title?that.title.trim():'';
			if(postParams.title == '')
			{
				that.showError('名称标题不能为空');
			}
			//判断电话号
			postParams.mobile = that.mobile;
			
			//判断时间
			postParams.start_time = that.$api.timeToNumber(that.mxDatePicker.start_time);
			//that.$api.msg(postParams.start_time);
			postParams.end_time = that.$api.timeToNumber(that.mxDatePicker.end_time);
			if(postParams.end_time && postParams.end_time < that.$api.getTime())
			{
				that.showError('结束时间不能小于当前时间');
			}else if(postParams.start_time && postParams.end_time && postParams.start_time > postParams.end_time)
			{
				that.showError('开始时间不能大于结束时间');
			}else if(!postParams.start_time && postParams.end_time)
			{
				that.showError('请选择开始时间');
			}else if(postParams.start_time && !postParams.end_time)
			{
				that.showError('请选择结束时间');
			}
			//判断推送圈子
			postParams.pushCircleStatus = that.pushCircleStatus;
			if(postParams.pushCircleStatus == 1)
			{
				if(that.pushCircleList.length <1)
				{
					postParams.pushCircleId = 0;
				}else{
					postParams.pushCircleId = that.pushCircleList[that.push_circle_index].circle_id;
				}
			}else{
				postParams.pushCircleId = 0;
			}
			//判断权限
			if(that.joinRuleList.length > 0)
			{
				postParams.show_rule_status = that.joinRuleList[that.joinRuleListIndex].id;
			}else{
				postParams.show_rule_status = 0;
			}
			//判断是否可以重复参加
			postParams.againMore = that.againMore;
			that.$api.showModal('确认发布','',async function(res){
				//判断需要填写的表单内容
				if(res != 'yes')
				{
					return false;
				}
				var text = '';var c_text = '';
				var dataList = that.$refs.show_form.getItemList();
				if(dataList.length > 0)
				{
					var opeList = that.$api.opeDiyFormData(dataList);
					if(opeList.length < 1)
					{
						return false;
					}
					 text = that.$refs.show_form.getItemListText();
				}
				postParams.dataList = dataList;
				//判断上传的表单内容
				var checkLength = that.$refs.add_sel_button.checkFormData();
				if(checkLength == -1)
				{
					//返回-1代表有错误发生了
					return false; 
				}
				if(checkLength > 0)
				{
					c_text = that.$refs.add_sel_button.getFormDataText();
				}
				var need_check_text = postParams.title +','+ c_text +','+ text;
				if(need_check_text)
				{
					var res = await that.$api.checkQiniuText(need_check_text);
					postParams.checkRes = res;
					if(!res.status)
					{
						return ;
					}
				}
				if(checkLength > 0)
				{
					//大于0表示，需要验证上传等
					var contentList = await that.$refs.add_sel_button.getContent();
					if(contentList.length < 1)
					{
						that.showError('获取内容失败');
					}
					postParams.contentList = contentList;
				}else{
					postParams.contentList = [];
				}
				//获取背景图
				var bgimg = await that.$refs.add_sel_bg.getBgUrl();
				if(!bgimg)
				{
					return false;
				}
				postParams.bg_image = bgimg.qnPath?bgimg.qnPath:'';
				that.$api.loadingMsg('正在创建');
				that.$http.post('statistics/create',postParams).then(res=>{
					that.$api.msg(res.msg);
					if(res.code == 1)
					{
						setTimeout(function(){
							uni.redirectTo({
								url:'/pages/form/statistics/detail?s_id='+res.data.s_id
							})
							//that.$api.navigateTo('/pages/form/statistics/detail?s_id='+res.data.s_id);
						},1000)
					}
					
				})
			})
			
			/* var bgimg = await this.$refs.add_sel_bg.getBgUrl();
			console.log(bgimg) */
			//var contentList = await this.$refs.add_sel_button.getContent();
			//console.log('contentList',contentList)
		},
		showError(text)
		{
			if(text)
			{
				this.$api.msg(text);
			}
			throw 'error';
		}
	}
}